package com.cx.gateway.utils;

import com.alibaba.fastjson.JSON;
import com.nimbusds.jose.JWSObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.PublicKey;
import java.security.cert.CertificateFactory;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhangcx
 * @Description TODO
 * @date 2022/6/7 13:20
 * @email zhangcx@bluemoon.com.cn
 */
@Slf4j
public class JWTUtils {
    private static final String PUBLIC_KEY = "public.key";
    private static String publickey = "";
    public static final String RESOURCE_ID = "res3";

    /**
     * 加载本地公钥
     *
     * @return
     */
    private static String getPublicKey() {
        if (StringUtils.isNotBlank(publickey)) {
            return publickey;
        }
        Resource resource = new ClassPathResource(PUBLIC_KEY);
        InputStreamReader inputStreamReader = null;
        BufferedReader reader = null;
        try {
            inputStreamReader = new InputStreamReader(resource.getInputStream());
            reader = new BufferedReader(inputStreamReader);
            publickey = reader.lines().collect(Collectors.joining("\n"));
            return publickey;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (null != inputStreamReader) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != reader) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
